package com.mybatis.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mybatis.Dao.IMemberMapper;
import com.mybatis.Util.MyBatisUtil;
import com.mybatis.Util.Result;
import com.mybatis.entity.Department;

/**
 * Servlet implementation class departmentServlet
 */
@WebServlet("/departmentServlet")
public class departmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public departmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");//设置输出信息编码方式为utf-8
		response.setContentType("text/json");//设置输出信息格式
		//调用查询部门信息方法
		IMemberMapper mdao = MyBatisUtil.getMapper(IMemberMapper.class);
		List<Department> list =mdao.queryByDepartment();
		//将执行结果存入结果集
		Result r = new Result(0,"查询成功",list.size(),list);
		//将结果集转为json格式
		Gson g = new Gson();
		String str = g.toJson(r);
		//输出结果集
		PrintWriter out =response.getWriter();
		out.print(str);
		//释放资源
		out.close();
		MyBatisUtil.closeSqlSession();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
